/**
 * <pre>
 * Title: 		MessageServiceImpl.java
 * Project: 	MSTS
 * Author:		pengfangliang
 * Create:	 	2012-7-4 上午11:24:39
 * Copyright: 	Copyright (c) 2012
 * Company:		Shenzhen Helper
 * <pre>
 */
package com.hengpeng.msls.service.message.impl;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.springframework.stereotype.Service;

import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.Restrictions;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.common.persist.query.model.Order;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.itfinbase.persist.Message;
import com.hengpeng.itfinbase.persist.MessageReceiver;
import com.hengpeng.msls.port.client.message.Constants.MessageType;
import com.hengpeng.msls.service.BaseService;
import com.hengpeng.msls.service.message.MessageService;

/**
 * <pre>
 * 站内信服务层接口实现
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2012-7-4
 */
@Service
public class MessageServiceImpl extends BaseService implements MessageService
{

	/*
	 * （非 Javadoc）
	 * @see com.hengpeng.msts.service.message.MessageService#findMessageWithPage()
	 */
	@Override
	public List<Object> findMessageWithPage(String betCardNo, MessageType type, Integer pageIndex, Integer pageSize,
			LinkedHashMap<String, OrderType> sort)
	{
		DynamicQuery query = new DynamicQuery(Message.class, "m").createAlias(MessageReceiver.class, "rm");
		query.eqProperty("m.id", "rm.messageId");
		query.add(Restrictions.ne("rm.isDelete", BoolValue.YES));
		query.add(Restrictions.eq("rm.betCardNo", betCardNo));
		query.add(Restrictions.eq("m.type", type));
		query.addProjection(Projections.property("rm.id"));
		query.addProjection(Projections.property("m.title"));
		query.addProjection(Projections.property("m.content"));
		query.addProjection(Projections.property("rm.isRead"));
		query.addProjection(Projections.property("m.publishDate"));
		query.setPageIndex(pageIndex);
		query.setPageSize(pageSize);
		query.setQueryType(QueryType.JDBC);
		if (sort != null)
		{
			for (Entry<String, OrderType> iterable_element : sort.entrySet())
			{
				if (iterable_element.getValue() == Order.OrderType.ASC)
				{
					query.asc(iterable_element.getKey());
				}
				else
				{
					query.desc(iterable_element.getKey());
				}
			}
		}
		System.out.println(query.getSqlResult().getSql());
		return serviceFactory.getProtFactory().findObject(null, query);
	}

	@Override
	public Long findMessageWithCount(String betCardNo, MessageType type)
	{
		DynamicQuery query = new DynamicQuery(Message.class, "m").createAlias(MessageReceiver.class, "rm");
		query.eqProperty("m.id", "rm.messageId");
		query.add(Restrictions.ne("rm.isDelete", BoolValue.YES));
		query.add(Restrictions.eq("rm.betCardNo", betCardNo));
		query.add(Restrictions.eq("m.type", type));
		query.addProjection(Projections.count());
		return serviceFactory.getProtFactory().count(null, query);
	}

}
